package com.wdg.service.impl;

import com.wdg.dao.IAroundeqDao;
import com.wdg.pojo.*;
import com.wdg.service.IAroundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class AroundServiceImpl implements IAroundService {

    @Autowired
    private IAroundeqDao aroundeqDao;

    @Override
    public int uploadTopic(Topic topic) {

        String tid = UUID.randomUUID().toString().replace("-","");

        topic.setTid(tid);

        for (TopicImage image : topic.getImage()){
            image.setTid(tid);
            addTopicImage(image);
        }
       return aroundeqDao.uploadTopic(topic);
    }

    @Override
    public int addTopicImage(TopicImage topicImage) {
        return aroundeqDao.addTopicImage(topicImage);
    }

    @Override
    public List<Topic> getTopic(int page) {

        //int p = page * 2 -1;

        List<Topic> topic = aroundeqDao.getTopic(page);

        for (Topic t:topic) {
            t.setImage(getTopicImage(t.getTid()));
            t.setComments(getAllComment(t.getTid()));
        }

        return topic;
    }

    @Override
    public List<TopicImage> getTopicImage(String tid) {
        return aroundeqDao.getTopicImage(tid);
    }

    @Override
    public List<Comment> getAllComment(String tid) {
        return aroundeqDao.getAllComment(tid);
    }

    @Override
    public User login(User user) {
        return aroundeqDao.login(user);
    }

    @Override
    public int regist(User user) {
        return aroundeqDao.regist(user);
    }

    @Override
    public List<Advert> getAllAdvert() {
        return aroundeqDao.getAllAdvert();
    }

    @Override
    public List<User> getAllUser(int page) {
        List<User> allUser = aroundeqDao.getAllUser(page);
        for (User user : allUser) {
            user.setFocusList(getFocesList(user.getUid()));
        }

        return allUser;
    }

    @Override
    public List<User> getAllUsers() {

        List<User> allUser = aroundeqDao.getAllUsers();

        for (User user : allUser) {
            user.setFocusList(getFocesList(user.getUid()));
        }

        return allUser;
    }

    @Override
    public boolean focus(int uid, int uided) {
        return aroundeqDao.focus(uid,uided) > 0;
    }

    @Override
    public boolean delfocus(int uid, int uided) {
        return aroundeqDao.delfocus(uid,uided) > 0;
    }

    @Override
    public boolean isfocus(int uid, int uided) {
        return aroundeqDao.isfocus(uid,uided) != null;
    }

    @Override
    public boolean staroff(int uid, String cbid) {
        return aroundeqDao.staroff(uid,cbid) > 0;
    }

    @Override
    public boolean delstaroff(int uid, String cbid) {
        return aroundeqDao.delstaroff(uid,cbid) > 0;
    }

    @Override
    public boolean isstaroff(int uid, String cbid) {
        return aroundeqDao.isstaroff(uid,cbid) != null;
    }

    @Override
    public List<focus> getFocesList(int uid) {
        return aroundeqDao.getFocesList(uid);
    }

    @Override
    public List<staroff> getStaroffList(String cbid) {
        return aroundeqDao.getStaroffList(cbid);
    }
}
